package bidNodeList;

import NodeList.*;
import iList.IList;

/**
 * @Author liangzai
 * @Description:
 */

//双向不带头链表

public class BidNodeList implements IList {

    public BidNode head;
    public BidNode last;


    @Override
    public void addFirst(int data) {
        if(head == null){
            BidNode bidNode = new BidNode(data);
            last = head = bidNode;
            return;
        }
        BidNode newnode = new BidNode(data);
        newnode.next = head;
        head.prev = newnode;
        head = newnode;
    }

    @Override
    public void addLast(int data) {
        BidNode node = new BidNode(data);
        if(head == null){
            last = head = node;
            return;
        }
        node.prev = last;
        last.next = node;
        last = node;
    }

    private void isIndex(int pos){
        if(pos<0 || pos>size()-1){
            throw new PosException("位置不合法！");
        }
    }

    @Override
    public void addIndex(int pos, int data) {
        try {
            isIndex(pos);
            if(pos == 0){
                addFirst(data);
                return ;
            }
            if(pos == size()-1){
                addLast(data);
                return ;
            }
            BidNode node = new BidNode(data);
            BidNode pcur = head;
            for (int i = 1; i < pos; i++) {
                pcur = pcur.next;
            }
            node.prev = pcur;
            node.next = pcur.next;
            pcur.next = node;
            node.next.prev = node;
        }catch (PosException e){
            System.out.println("位置不合法！");
            e.printStackTrace();
        }
    }

    @Override
    public boolean contains(int key) {
        BidNode ahead = head;
        while(ahead!=null){
            if(ahead.val == key){
                return true;
            }
        }
        return false;
    }

    private void isEmpty(){
        if(head == null){
            throw new EmptyException("链表为空！");
        }
    }

    private void removeFrist(){
        head = head.next;
        head.prev = null;
    }

    private void removeLast(){
        if(head == last){
            head = null;
        }else{
            last = last.prev;
            last.next = null;
        }
    }

    @Override
    public void remove(int key) {
        try {
            isEmpty();
            if(head.val == key){
                removeFrist();
                return ;
            }
            if(last.val == key){
                removeLast();
                return ;
            }

            BidNode pcur = head.next;
            while(pcur!=null && pcur != last){
                if(pcur.val == key){
                    BidNode aprev = pcur.prev;
                    BidNode next = pcur.next;
                    aprev.next = next;
                    next.prev = aprev;
                    pcur = null;
                    return;
                }
                pcur = pcur.next;
            }
            System.out.println("此数据不存在！");
        }catch (EmptyException e){
            System.out.println("链表为空！");
            e.printStackTrace();
        }
    }

    @Override
    public void removeAllKey(int key) {

            BidNode pcur = head.next;
            while(pcur!=null && pcur!=last) {

                if (pcur.val == key) {
                    BidNode aprev = pcur.prev;
                    BidNode next = pcur.next;
                    aprev.next = next;
                    next.prev = aprev;

                }
                pcur = pcur.next;
        }
        if (head.val == key) {
            removeFrist();
        }
        if (last.val == key) {
            removeLast();

        }
    }

    @Override
    public int size() {
        BidNode ahead = head;
        int count=0;
        while(ahead!=null){
            count++;
            ahead = ahead.next;
        }
        return count;
    }

    @Override
    public void clear() {
        BidNode pcur = head;
        while(pcur!=null){
            BidNode nextpcur = pcur.next;
            pcur = null;
            pcur = nextpcur;
        }
        head = null;
    }

    @Override
    public void display() {
        BidNode ahead = head;
        while(ahead!=null){
            System.out.print(ahead.val+" ");
            ahead = ahead.next;
        }
        System.out.println();
    }
}
